<template>
  <div>
    <div style="height: 1000px">
      <FormPanel
        ref="formPanel"
        :cols="2"
        :labelWidth="110"
        :list="formList"
        :formType="type"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
        style="margin-top: 20px"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" @click="saveDialog" type="primary">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import {
  getOrgsTree,
  addUserInfo,
  editUserInfo,
  getUserInfoById,
  getComOrgTreeList, } from '@/api/common/org/employeeInfo';
import { dictionary } from '@/utils/dictMixin';
import { getJsonTree } from '@/utils/index';
import { IDNumber, phone } from '@/utils/validate'
export default {
  name: 'EmpPanel',
  mixins: [dictionary],
  components: {},
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    return {
      visible: false,
      formList: this.createFormList()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.inputTree();
    this.init();
  },
  methods: {
    init() {
      if (this.type === 'edit' || this.type === 'show') {
        // 更新接口需要的记录 ID
        this.empId = this.rowData.employeeId;
        getUserInfoById('',this.rowData.employeeId).then(res=>{
          this.formList.forEach(x => {
            x.initialValue = res.data[x.fieldName];
          });
        }).catch(err=>{})
        
        console.log("this.rowData.orgId",this.rowData.orgId,this.rowData.orgName)
        this.formList[2].initialValue=this.rowData.orgId
        this.formList[2].placeholder=this.rowData.orgName
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '员工编号',
          fieldName: 'employeeNo',
          placeholder: '请输入员工编号',
          initialValue: '',
          rules: [{ required: true, message: '请输入员工编号', trigger: 'blur' }, { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '员工姓名',
          fieldName: 'employeeName',
          placeholder: '请输入员工姓名',
          initialValue: '',
          rules: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }]
        },
        {
          type: 'INPUT_TREE',
          label: '上级部门',
          fieldName: 'orgId',
          placeholder: '上级部门',
          initialValue: '',
          itemList: [],
          // rules: [{ required: true, message: '请选择上级部门', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '性别',
          fieldName: 'gender',
          placeholder: '请选择',
          itemList: this.createDictList('1002')
        },
        {
          type: 'DATE',
          label: '出生日期',
          fieldName: 'birthday',
          placeholder: '请选择'
        },
        {
          type: 'INPUT',
          label: '身份证号',
          fieldName: 'certificateId',
          placeholder: '身份证号',
          initialValue: '',
          rules: [{ required: false, validator: IDNumber, trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '手机',
          fieldName: 'phone',
          placeholder: '手机',
          initialValue: ''
        },
        // {
        //   type: 'INPUT',
        //   label: '电话',
        //   fieldName: 'mobile',
        //   placeholder: '电话',
        //   initialValue: ''
        // },
        {
          type: 'INPUT',
          label: 'eMail',
          fieldName: 'eMail',
          placeholder: 'eMail',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '邮编',
          fieldName: 'zipCode',
          placeholder: '邮编',
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '在职状态',
          fieldName: 'isOnjob',
          placeholder: '请输入',
          itemList: this.createDictList('1008')
        }/* ,
        {
          type: 'MULTIPLE_CHECKBOX',
          label: '技能信息',
          fieldName: 'employeeRoles',
          placeholder: '请输入',
          itemList: this.createDictList('1006')
        } */
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          res = await addUserInfo(this.saveData);
        }
        if (this.type === 'edit') {
          res = await editUserInfo(this.saveData, this.empId);
        }
      console.log(res,"res")
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }else{
          this.$message(`${res.errMsg}`);
        }
      }
    },
    closeDialog(type) {
      this.$emit('close', false, type);
    },
    async inputTree() {
      const res = await getComOrgTreeList();
      var that = res.data
      that.forEach(el => (el.text = el.text));
      that.forEach(el => (el.value = el.orgId));
      that.forEach(el => (el.parentId = el.parent));
       //console.log('员工树', getJsonTree(that));
      this.formList[2].itemList = getJsonTree(that);
      this.formList[2].initialValue=this.rowData.orgId
      this.formList[2].placeholder=this.rowData.orgName
    }
  }
};
</script>
